package com.sunny.excel.exception;

import lombok.Getter;
import lombok.Setter;

import java.util.Arrays;

/**
 * @Author:shucq
 * @Description:
 * @Date 2019/9/9
 */
@Getter
@Setter
public class BizException extends RuntimeException {
    private ErrorMessage errorMessage;
    private Object[] parameters;
    private Object data;


    public static BizException build(ErrorMessage errorMessage) {
        return new BizException(errorMessage);
    }

    public BizException(ErrorMessage errorMessage) {
        this.errorMessage = errorMessage;
    }

    public BizException(ErrorMessage errorMessage, Throwable t) {
        super(errorMessage.getMessage(), t);
        this.errorMessage = errorMessage;
    }

    public BizException(ErrorMessage errorMessage, Object... parameters) {
        this.errorMessage = errorMessage;
        this.parameters = parameters;
    }
    public String getCode() {
        return errorMessage.getCode();
    }
    public String getMessageKey() {
        return errorMessage.getMessageKey();
    }
    @Override
    public String getMessage() {
        return ((getParameters() == null) ?
            errorMessage.getMessage() :
            String.format(errorMessage.getMessage(), getParameters()));
    }

    @Override
    public String toString() {
        return String.format("ErrorCode: %s, ErrorMessage: %s, Parameters: %s", errorMessage.getCode(),
            errorMessage.getMessage(),
            (parameters == null || parameters.length == 0) ? "" : Arrays.asList(parameters).toString());
    }
}
